package db.migrations;

import static com.eroi.migrate.Define.autoincrement;
import static com.eroi.migrate.Define.column;
import static com.eroi.migrate.Define.length;
import static com.eroi.migrate.Define.notnull;
import static com.eroi.migrate.Define.primarykey;
import static com.eroi.migrate.Define.table;
import static com.eroi.migrate.Define.DataTypes.INTEGER;
import static com.eroi.migrate.Define.DataTypes.LONGVARCHAR;
import static com.eroi.migrate.Define.DataTypes.TIMESTAMP;
import static com.eroi.migrate.Define.DataTypes.VARCHAR;
import static com.eroi.migrate.Execute.createTable;
import static com.eroi.migrate.Execute.dropTable;

import com.eroi.migrate.Migration;

public class Migration_1 implements Migration {

	@Override
	public void up() {
		createTable(table("catalog", 
				column("id",INTEGER, primarykey(),notnull(),length(11),autoincrement()),
				column("cat_name", VARCHAR, length(32)),
				column("item_id", INTEGER, length(11)),
				column("order", INTEGER, length(11)),
				column("addtime", TIMESTAMP)
				));
		createTable(table("item", 
				column("id",INTEGER, primarykey(),notnull(),length(11),autoincrement()),
				column("item_name", VARCHAR, length(50)),
				column("item_description", VARCHAR, length(255)),
				column("uid", INTEGER, length(11)),
				column("username", VARCHAR, length(50)),
				column("password", VARCHAR, length(32)),
				column("addtime", TIMESTAMP)
				));
		createTable(table("item_member", 
				column("id",INTEGER, primarykey(),notnull(),length(11),autoincrement()),
				column("item_id", INTEGER, length(11)),
				column("uid", INTEGER, length(11)),
				column("username", VARCHAR, length(50)),
				column("addtime", TIMESTAMP)
				));
		createTable(table("page", 
				column("id",INTEGER, primarykey(),notnull(),length(11),autoincrement()),
				column("author_uid", INTEGER, length(11)),
				column("author_username", VARCHAR, length(11)),
				column("item_id", INTEGER, length(11)),
				column("cat_id", INTEGER, length(11)),
				column("page_title", VARCHAR, length(50)),
				column("page_content", LONGVARCHAR),
				column("order", INTEGER, length(50)),
				column("addtime", TIMESTAMP)
				));
		
		createTable(table("page_history", 
				column("id",INTEGER, primarykey(),notnull(),length(11),autoincrement()),
				column("page_id", INTEGER, length(11)),
				column("author_uid", INTEGER, length(11)),
				column("author_username", VARCHAR, length(11)),
				column("item_id", INTEGER, length(11)),
				column("cat_id", INTEGER, length(11)),
				column("page_title", VARCHAR, length(50)),
				column("page_content", LONGVARCHAR),
				column("order", INTEGER, length(50)),
				column("addtime", TIMESTAMP)
				));
		createTable(table("user", 
				column("id",INTEGER, primarykey(),notnull(),length(11),autoincrement()),
				column("username", VARCHAR, length(20)),
				column("groupid", INTEGER, length(2)),
				column("name", VARCHAR, length(30)),
				column("avatar", VARCHAR, length(200)),
				column("avatar_small", VARCHAR, length(200)),
				column("email", VARCHAR, length(20)),
				column("password", VARCHAR, length(32)),
				column("cookie_token", VARCHAR, length(32)),
				column("cookie_token_expire", INTEGER, length(11)),
				column("reg_time", TIMESTAMP),
				column("last_login_time", TIMESTAMP)
				));
		
	}
	
	@Override
	public void down() {
		dropTable("catalog");
		dropTable("item");
		dropTable("item_member");
		dropTable("page");
		dropTable("page_history");
		dropTable("user");
	}
}
